// pages/320/320.js
var amapFile = require('../../libs/amap-wx.130.js');
import Toast from '@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    spotname:'',
    spotid:'',
    gonelist:'',
    show:false,
    city:'',
    markers:[],
    flag:true,
    value:0,
    rateok:false,
    myrate:0
  },
onChange(e){
  this.setData({value:e.detail})
},
submit(){
  if(getApp().globalData.user.id){
    wx.request({
      url: 'http://localhost:3000/spots/rate',
      data:{
        spotid:this.data.spotid,
        userid:getApp().globalData.user.id,
        value:this.data.value
      },
      success:res=>{
        console.log(res)
        Toast('评分成功')
        this.setData({
          show:false
        })
        wx.request({
          url: 'http://localhost:3000/spots/check',
          method: 'GET',
          data: {
            "spotid":this.data.spotid,
            'userid':getApp().globalData.user.id
          },
          success:res=>{
            console.log('look',res.data)
            let flag=false
            if(res.data.gone.length==0){
              flag=true
            }
            else{
              flag=false
            }
            if(res.data.rate){
              this.setData({
                rateok:true,
                myrate:res.data.rate.value
              })
            }
            this.setData({
              gonelist:res.data.gone,
              flag:flag
            })
          },
          fail:err=>{
            this.setData({
              flag:false
            })
            console.log(err)
          }
        }),
        wx.request({
          url: 'http://localhost:3000/spots/detail',
          method: 'GET',
          data: {
            "id":this.data.spotid
          },
          success:res=>{
            this.setData({detail:res.data})
            
          },
          fail:err=>{
            console.log(err)
          }
        })

      }
    })
  }
  else{
    Toast('请先登录')
    this.setData({
      show:false
    })
  }
  console.log(this.data.value)
  
},
  /**
   * 生命周期函数--监听页面加载
   */
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  onLoad(options) {
    let id=options.id;
    let that=this
    var myAmapFun = new amapFile.AMapWX({key: '038c54183637f97bf87484c0ce1c7b01'});

    wx.request({
      url: 'http://localhost:3000/spots/detail',
      method: 'GET',
      data: {
        "id":id
      },
      success:res=>{
        console.log(res);
        this.setData({
          detail:res.data,
          spotid:res.data.id,
          spotname:res.data.name,
          latitude:res.data.latitude,
          logitude:res.data.longitude
        })
        wx.getLocation({
          altitude: 'altitude',
          success:function(data){
            getApp().globalData.origina=data.latitude
            getApp().globalData.originb=data.longitude
            getApp().globalData.desa=Number(res.data.latitude)
            getApp().globalData.desb=Number(res.data.longitude)
            that.setData({
              latitude:Number(res.data.latitude),
              longitude:Number(res.data.longitude)
            })
            that.setData({
              markers:[
                {
                  id: 0,
                  latitude: getApp().globalData.desa,
                  longitude: getApp().globalData.desb,
                  width: 23,
                  height: 33
                }]})
            let origin=getApp().globalData.originb+','+getApp().globalData.origina
            let des=getApp().globalData.desb+','+getApp().globalData.desa
            myAmapFun.getRegeo({
              location: origin,//location的格式为'经度,纬度'
              success: function (data) {
                let {province,city} = data[0].regeocodeData.addressComponent;
                city = (city || city.length>0) ? city:"";
                console.log(province+city)
                getApp().globalData.origincity=province+city    
              },
              fail: function (info) { }
            })
            try{
            myAmapFun.getRegeo({
              location: des,//location的格式为'经度,纬度'
              success: function (data) {
                console.log('data',data)
                let {province,city,district} = data[0].regeocodeData.addressComponent;
                city = (city || city.length>0) ? city:"";
                getApp().globalData.descity=province+city
                that.setData({
                  city:data[0].regeocodeData.formatted_address
                })
                console.log('city',that.data.city)
              },
              fail: function (info) { 
                console.log(info)
              }
            })}
            catch(error){
              console.error(error)
            }       
          }})
        wx.request({
          url: 'http://localhost:3000/spots/check',
          method: 'GET',
          data: {
            "spotid":this.data.spotid,
            'userid':getApp().globalData.user.id
          },
          success:res=>{
            console.log('look',res.data)
            let flag=false
            if(res.data.gone.length==0){
              flag=true
            }
            else{
              flag=false
            }
            if(res.data.rate){
              this.setData({
                rateok:true,
                myrate:res.data.rate.value
              })
            }
            this.setData({
              gonelist:res.data.gone,
              flag:flag
            })
          },
          fail:err=>{
            this.setData({
              flag:false
            })
            console.log(err)
          }
        })
      },
      fail:err=>{
        console.log(err)
      }
    })


    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ show: false });
  },
  noop() {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  go(){
    wx.navigateTo({
      url: '../321/321?id='+this.data.spotid+'&name='+this.data.spotname+'&latitude='+this.data.latitude+'&longitude='+this.data.logitude,
    })
  },
  mark(){
    let date=new Date();
    date=this.formatDate(date)
    console.log(date)
    if(getApp().globalData.user.id){
      wx.request({
        url: 'http://localhost:3000/spots/addgone',
        method: 'GET',
        data: {
          spotid:this.data.spotid,
          userid:getApp().globalData.user.id,
          time:date
        },
        success:res=>{
          Toast('点亮成功')
          wx.request({
            url: 'http://localhost:3000/spots/check',
            method: 'GET',
            data: {
              "spotid":this.data.spotid,
              'userid':getApp().globalData.user.id
            },
            success:res=>{
              console.log(res.data)
              this.setData({
                gonelist:res.data.gone,
                flag:false
              })
            },
            fail:err=>{
              console.log(err)
            }
          })
        },
        fail:err=>{
          
        }
      })
    }
    else{
      Toast('请先登录')
    }
    
  },
  tips(){
    wx.navigateTo({
      url: '../410/410?id='+this.data.spotid+'&name='+this.data.spotname+'&latitude='+this.data.latitude+'&longitude='+this.data.logitude+'&pic='+this.data.detail.image,
    })
  },
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }

    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ':' + second()

},
})
